package org.jeecg.modules.exTender.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.exOnlineQuestioning.entity.ExOnlineQuestioning;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.service.IExSectionService;
import org.jeecg.modules.exTender.entity.ExTenderCandidateAnnounce;
import org.jeecg.modules.exTender.service.IExTenderCandidateAnnounceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.gcProject.vo.StatusEnum;
import org.jeecg.modules.website.entity.WebTradeNotice;
import org.jeecg.modules.website.mapper.WebTradeNoticeMapper;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 中标候选人公示
 * @Author: jeecg-boot
 * @Date:   2024-11-28
 * @Version: V1.0
 */
@Api(tags="中标候选人公示")
@RestController
@RequestMapping("/exTender/exTenderCandidateAnnounce")
@Slf4j
public class ExTenderCandidateAnnounceController extends JeecgController<ExTenderCandidateAnnounce, IExTenderCandidateAnnounceService> {
	@Autowired
	private IExTenderCandidateAnnounceService exTenderCandidateAnnounceService;
	@Autowired
	private IExSectionService exSectionService;
	 @Autowired
	 private WebTradeNoticeMapper webTradeNoticeMapper;
	/**
	 * 分页列表查询
	 *
	 * @param exTenderCandidateAnnounce
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "中标候选人公示-分页列表查询")
	@ApiOperation(value="中标候选人公示-分页列表查询", notes="中标候选人公示-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ExTenderCandidateAnnounce>> queryPageList(ExTenderCandidateAnnounce exTenderCandidateAnnounce,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ExTenderCandidateAnnounce> queryWrapper = QueryGenerator.initQueryWrapper(exTenderCandidateAnnounce, req.getParameterMap());
		Page<ExTenderCandidateAnnounce> page = new Page<ExTenderCandidateAnnounce>(pageNo, pageSize);
		IPage<ExTenderCandidateAnnounce> pageList = exTenderCandidateAnnounceService.page(page, queryWrapper);
		if(pageList.getRecords().size()>0){
			List<String> sectionIdList = pageList.getRecords().stream().map(ExTenderCandidateAnnounce::getBidSectionId).collect(Collectors.toList());
			List<ExSection> exSectionList = exSectionService.selectByIds(sectionIdList.toArray(new String[0]));
			if (CollectionUtil.isNotEmpty(exSectionList)) {
				pageList.getRecords().forEach((item -> {
					Optional<ExSection> sectionOptional = exSectionList.stream().filter(obj -> obj.getId().equals(item.getBidSectionId())).findFirst();
					if (sectionOptional.isPresent()) {
						item.setBidSectionName(sectionOptional.get().getBidSectionName());
					}
				}));
			}
		}
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param exTenderCandidateAnnounce
	 * @return
	 */
	@AutoLog(value = "中标候选人公示-添加")
	@ApiOperation(value="中标候选人公示-添加", notes="中标候选人公示-添加")
	//@RequiresPermissions("org.jeecg.modules:ex_tender_candidate_announce:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ExTenderCandidateAnnounce exTenderCandidateAnnounce) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		exTenderCandidateAnnounce.setCreateBy(sysUser.getId());
		exTenderCandidateAnnounce.setCreateTime(new Date());
		exTenderCandidateAnnounce.setStatus(StatusEnum.SAVE.getCode());
		//官网插入数据
		WebTradeNotice webTradeNotice = new WebTradeNotice();
		webTradeNotice.setId(exTenderCandidateAnnounce.getId());
		webTradeNotice.setCategory("sys_trade_type_jgcg");//建设工程大类
		/**缺行政区域标志  webTradeNotice.setArea(exTenderAnnQuaInqueryAnn.)**/
		webTradeNotice.setTitile(exTenderCandidateAnnounce.getPublicityName());//公示标题
		webTradeNotice.setContent(exTenderCandidateAnnounce.getPublicityContent());//公示内容
		webTradeNotice.setPublishTime(exTenderCandidateAnnounce.getCreateTime());//发布时间
		webTradeNotice.setOpenType("component");//发布方式默认
		webTradeNotice.setOpenPage(exTenderCandidateAnnounce.getUrl());//URL地址
		webTradeNotice.setPublisher("开封市公共资源交易信息网");//默认发布人
		webTradeNotice.setType(exTenderCandidateAnnounce.getPublicityType());//公式类型
		if (webTradeNotice.getType() == "4") {
			webTradeNotice.setTypeDesc("评标公示");
		}
		if (webTradeNotice.getType() == "5") {
			webTradeNotice.setTypeDesc("中标候选人公示");
		}
		if (webTradeNotice.getType() == "7") {
			webTradeNotice.setTypeDesc("合同公示");
		}
		if (webTradeNotice.getType() == "8") {
			webTradeNotice.setTypeDesc("履约公示");
		}
		webTradeNotice.setPublishStatus("1");//默认已发布
		webTradeNotice.setPriority("H");//默认优先级高
		webTradeNotice.setArea(exTenderCandidateAnnounce.getPlatformCode());//行政区域
		webTradeNoticeMapper.insert(webTradeNotice);
		//官网插入数据
		ExSection exSection = exSectionService.getById(exTenderCandidateAnnounce.getBidSectionId());
		exTenderCandidateAnnounce.setProjectId(exSection.getProjectId());
		exTenderCandidateAnnounceService.save(exTenderCandidateAnnounce);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param exTenderCandidateAnnounce
	 * @return
	 */
	@AutoLog(value = "中标候选人公示-编辑")
	@ApiOperation(value="中标候选人公示-编辑", notes="中标候选人公示-编辑")
	//@RequiresPermissions("org.jeecg.modules:ex_tender_candidate_announce:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ExTenderCandidateAnnounce exTenderCandidateAnnounce) {
		exTenderCandidateAnnounceService.updateById(exTenderCandidateAnnounce);
		//官网插入数据
		webTradeNoticeMapper.deleteById(exTenderCandidateAnnounce.getId());
		WebTradeNotice webTradeNotice = new WebTradeNotice();
		webTradeNotice.setId(exTenderCandidateAnnounce.getId());
		webTradeNotice.setCategory("sys_trade_type_jgcg");//建设工程大类
		/**缺行政区域标志  webTradeNotice.setArea(exTenderAnnQuaInqueryAnn.)**/
		webTradeNotice.setTitile(exTenderCandidateAnnounce.getPublicityName());//公示标题
		webTradeNotice.setContent(exTenderCandidateAnnounce.getPublicityContent());//公示内容
		webTradeNotice.setPublishTime(exTenderCandidateAnnounce.getCreateTime());//发布时间
		webTradeNotice.setOpenType("component");//发布方式默认
		webTradeNotice.setOpenPage(exTenderCandidateAnnounce.getUrl());//URL地址
		webTradeNotice.setPublisher("开封市公共资源交易信息网");//默认发布人
		webTradeNotice.setType(exTenderCandidateAnnounce.getPublicityType());//公式类型
		if (webTradeNotice.getType() == "4") {
			webTradeNotice.setTypeDesc("评标公示");
		}
		if (webTradeNotice.getType() == "5") {
			webTradeNotice.setTypeDesc("中标候选人公示");
		}
		if (webTradeNotice.getType() == "7") {
			webTradeNotice.setTypeDesc("合同公示");
		}
		if (webTradeNotice.getType() == "8") {
			webTradeNotice.setTypeDesc("履约公示");
		}
		webTradeNotice.setPublishStatus("1");//默认已发布
		webTradeNotice.setPriority("H");//默认优先级高
		webTradeNotice.setArea(exTenderCandidateAnnounce.getPlatformCode());//行政区域
		webTradeNoticeMapper.insert(webTradeNotice);
		//官网插入数据

		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "中标候选人公示-通过id删除")
	@ApiOperation(value="中标候选人公示-通过id删除", notes="中标候选人公示-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:ex_tender_candidate_announce:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		exTenderCandidateAnnounceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "中标候选人公示-批量删除")
	@ApiOperation(value="中标候选人公示-批量删除", notes="中标候选人公示-批量删除")
	//@RequiresPermissions("org.jeecg.modules:ex_tender_candidate_announce:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.exTenderCandidateAnnounceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "中标候选人公示-通过id查询")
	@ApiOperation(value="中标候选人公示-通过id查询", notes="中标候选人公示-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ExTenderCandidateAnnounce> queryById(@RequestParam(name="id",required=true) String id) {
		ExTenderCandidateAnnounce exTenderCandidateAnnounce = exTenderCandidateAnnounceService.getById(id);
		if(exTenderCandidateAnnounce==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(exTenderCandidateAnnounce);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param exTenderCandidateAnnounce
    */
    //@RequiresPermissions("org.jeecg.modules:ex_tender_candidate_announce:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExTenderCandidateAnnounce exTenderCandidateAnnounce) {
        return super.exportXls(request, exTenderCandidateAnnounce, ExTenderCandidateAnnounce.class, "中标候选人公示");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("ex_tender_candidate_announce:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ExTenderCandidateAnnounce.class);
    }

}
